package com.dinzeer.godfox.effect;

import com.dinzeer.godfox.regsiter.GBEffectRegsitry;
import com.dinzeer.godfox.regsiter.GBSpecialEffectsRegistry;
import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingHealEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.awt.*;
import java.util.UUID;

@Mod.EventBusSubscriber
public class ForTheBlood extends MobEffect {

    // UUIDs for attribute modifiers
    private static final UUID SPEED_MODIFIER_UUID = UUID.nameUUIDFromBytes("SPEED_MODIFIER_UUID_ForTheBlood".getBytes());
    private static final UUID DAMAGE_MODIFIER_UUID = UUID.nameUUIDFromBytes("DAMAGE_MODIFIER_UUID_ForTheBlood".getBytes());

    public ForTheBlood() {
        super(MobEffectCategory.BENEFICIAL, Color.RED.getRGB());
    }

    @Override
    public void applyEffectTick(LivingEntity entity, int amplifier) {
        if (!(entity instanceof Player player)) return;

        float healthPercentage = player.getHealth() / player.getMaxHealth();

        // Remove all modifiers first to avoid stacking
        player.getAttribute(Attributes.MOVEMENT_SPEED).removeModifier(SPEED_MODIFIER_UUID);
        player.getAttribute(Attributes.ATTACK_DAMAGE).removeModifier(DAMAGE_MODIFIER_UUID);

        if (healthPercentage > 0.75f) {
            // 生命值 > 75%: 减少50%速度
            AttributeModifier speedReduction = new AttributeModifier(
                    SPEED_MODIFIER_UUID,
                    "ForTheBlood_speed_reduction",
                    -0.5,
                    AttributeModifier.Operation.MULTIPLY_TOTAL
            );
            player.getAttribute(Attributes.MOVEMENT_SPEED).addTransientModifier(speedReduction);
        } else if (healthPercentage < 0.25f) {
            // 生命值 < 25%: 攻击伤害+12，速度提升100%
            AttributeModifier damageBoost = new AttributeModifier(
                    DAMAGE_MODIFIER_UUID,
                    "ForTheBlood_damage_boost",
                    12.0,
                    AttributeModifier.Operation.ADDITION
            );
            AttributeModifier speedBoost = new AttributeModifier(
                    SPEED_MODIFIER_UUID,
                    "ForTheBlood_speed_boost",
                    1.0,
                    AttributeModifier.Operation.MULTIPLY_TOTAL
            );
            player.getAttribute(Attributes.ATTACK_DAMAGE).addTransientModifier(damageBoost);
            player.getAttribute(Attributes.MOVEMENT_SPEED).addTransientModifier(speedBoost);
        }
    }

    @Override
    public boolean isDurationEffectTick(int duration, int amplifier) {
        // 每tick都执行效果检查
        return true;
    }

    @SubscribeEvent
    public static void onLivingHurt(LivingHurtEvent event) {
        LivingEntity entity = event.getEntity();

        if (entity instanceof Player player && player.hasEffect(GBEffectRegsitry.ForTheBlood.get())) {
            float healthPercentage = player.getHealth() / player.getMaxHealth();

            if (healthPercentage > 0.75f) {
                // 生命值 > 75%: 减免20%伤害
                float reducedDamage = event.getAmount() * 0.8f;
                event.setAmount(reducedDamage);
            }
        }
    }

    @SubscribeEvent
    public static void onLivingDamage(LivingDamageEvent event) {
        LivingEntity attacker = event.getSource().getEntity() instanceof LivingEntity ?
                (LivingEntity) event.getSource().getEntity() : null;

        // 检查攻击者是否有ForTheBlood效果且生命值<25%
        if (attacker instanceof Player player && player.hasEffect(GBEffectRegsitry.ForTheBlood.get())) {
            float healthPercentage = player.getHealth() / player.getMaxHealth();

            if (healthPercentage < 0.25f ) {
                // 近战伤害的33%转为伤害吸收护盾
                float damageAmount = event.getAmount();
                float absorptionAmount = damageAmount * 0.33f;

                // 给攻击者添加吸收护盾
                float currentAbsorption = player.getAbsorptionAmount();
                if (player.getAbsorptionAmount()<5*20){
                player.setAbsorptionAmount(currentAbsorption + absorptionAmount);
                }
            }
        }
    }
    @SubscribeEvent
    public static void LivingHurt(LivingHurtEvent event) {



            if (SlashBladeUtil.hasSpecialEffect(event.getEntity().getMainHandItem(), GBSpecialEffectsRegistry.BloodFoxCurse)){

                if (event.getAmount()>event.getEntity().getMaxHealth()*0.09){
                   
                    event.setAmount(event.getEntity().getMaxHealth()*0.09f);
                }
            }


    }

    @SubscribeEvent
    public static void onLivingHeal(LivingHealEvent event) {
        LivingEntity entity = event.getEntity();

        if (entity instanceof Player player && player.hasEffect(GBEffectRegsitry.ForTheBlood.get())) {
            float healthPercentage = player.getHealth() / player.getMaxHealth();

            if (healthPercentage < 0.25f) {
                // 生命值 < 25%: 锁定生命值，阻止恢复
                event.setCanceled(true);
            }
        }
    }
}